#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string>
#include"RingQueue.hpp"
#include"Task.hpp"
using namespace std;

struct ThreadData
{
    RingQueue<Task>* rq;
    string threadname;
};

void *Productor(void *args)
{
    ThreadData *td=static_cast<ThreadData*>(args);
    RingQueue<Task> *rq=td->rq;
    int len=opers.size();
    while(true)
    {
        //1. 获取数据
        int data1=rand()%10+1;
        usleep(10);
        int data2=rand()%10+1;
        char op=opers[rand()%len];
        Task t(data1,data2,op);
        
        //2. 生产数据       
        rq->Push(t);
        cout<<"Product Task done, Task is: "<<t.GetTask()<<" who: "<<td->threadname<<endl;
    }
    return nullptr;
}
void *Consumer(void *args)
{
    ThreadData *td=static_cast<ThreadData*>(args);
    RingQueue<Task> *rq=td->rq;
    while(true)
    {
        //1. 消费数据
        Task t;
        rq->Pop(&t);
        t.run();
        cout<<"Consumer get Task done, Task is: "<<t.GetResult()<<" who: "<<td->threadname<<endl;
        sleep(1);
        //2. 处理数据

    }
    return nullptr;
}
int main()
{
    srand(time(nullptr)^getpid());
    RingQueue<Task> *rq=new RingQueue<Task>();

    pthread_t c[1],p[1];

    for(int i=0;i<1;i++)
    {
        ThreadData *td=new ThreadData();
        td->rq=rq;
        td->threadname="Productor: "+to_string(i);
        pthread_create(c+i,nullptr,Productor,td);
    }
    for(int i=0;i<1;i++)
    {
        ThreadData *td=new ThreadData();
        td->rq=rq;
        td->threadname="Consumer: "+to_string(i);
        pthread_create(p+i,nullptr,Consumer,td);
    }
    
    for(int i=0;i<1;i++)
    {
        pthread_join(c[i],nullptr);
    }
    for(int i=0;i<1;i++)
    {
        pthread_join(c[i],nullptr);
    }

    return 0;
}